﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Danser_AJAXEnabledWebApplication.Resourses;
using Danser_Control.Managers;
using Danser_Entity.Entities;

namespace Danser_AJAXEnabledWebApplication.Query_Popups
{
    /// <summary>
    /// class description...
    /// </summary>
    public partial class SaveQuery : System.Web.UI.Page
    {
        #region Constants:

        /// <summary>
        /// String
        /// </summary>
        public const String SESSION_RULE_TO_SAVE_ID = "RuleToSave";
        /// <summary>
        /// String
        /// </summary>
        public const String SESSION_USER_ID = "SaveQueryUserId";

        #endregion

        #region Page Load:

        /// <summary>
        /// loads the page...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) //first time
            {
                ruleLabel.Text = Session[SESSION_RULE_TO_SAVE_ID] as String;
            }
        }

        #endregion

        #region Save Query:

        /// <summary>
        /// event handler - save the selected query t0 DB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void saveQueryButton_Click(object sender, EventArgs e)
        {
            IQueryManager queryManager = ManagerFactory.QueryManager();
            String userID = Session[SESSION_USER_ID] as String;
            String queryName = nameTextBox.Text;
            String queryDesc = descriptionTextBox.Text;
            String queryString = ruleLabel.Text;

            queryManager.SaveQuery(queryName, queryDesc, queryString, userID);

            //manage UI elements 
            String message = String.Format(UIMessages.SavedQuery, new object[] { queryName });
            confirmSavingLabel.Text = message;
            savePanel.Visible = false;
            confirmSavingPanel.Visible = true;
        }

        #endregion
    }
}
